<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="笼架" prop="cageRackId">
        <el-select v-model="queryParams.cageRackId" placeholder="请选择笼架" clearable>
          <el-option
            v-for="dict in dict.type.cage_rack_id"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="在笼动物" prop="animalId">
        <el-input
          v-model="queryParams.animalId"
          placeholder="请输入在笼动物"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="在笼数量" prop="animalNum">
        <el-input
          v-model="queryParams.animalNum"
          placeholder="请输入在笼数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属人员" prop="manId">
        <el-input
          v-model="queryParams.manId"
          placeholder="请输入所属人员"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属实验" prop="exId">
        <el-input
          v-model="queryParams.exId"
          placeholder="请输入所属实验"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="笼位类型" prop="cageTypeId">
        <el-select v-model="queryParams.cageTypeId" placeholder="请选择笼位类型" clearable>
          <el-option
            v-for="dict in dict.type.cage_type_id"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="笼位规格" prop="cageSize">
        <el-input
          v-model="queryParams.cageSize"
          placeholder="请输入笼位规格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="笼位位置" prop="position">
        <el-input
          v-model="queryParams.position"
          placeholder="请输入笼位位置"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="笼位状态" prop="cageStatus">
        <el-select v-model="queryParams.cageStatus" placeholder="请选择笼位状态" clearable>
          <el-option
            v-for="dict in dict.type.cage_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="生物安全等级" prop="safetyLevel" label-width="12">
        <el-select v-model="queryParams.safetyLevel" placeholder="请选择生物安全等级" clearable>
          <el-option
            v-for="dict in dict.type.safety_level"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['lmisCage:cage:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['lmisCage:cage:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['lmisCage:cage:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['lmisCage:cage:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="cageList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="笼架" align="center" prop="cageRackId">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cage_rack_id" :value="scope.row.cageRackId"/>
        </template>
      </el-table-column>
      <el-table-column label="在笼动物" align="center" prop="animalId" />
      <el-table-column label="在笼数量" align="center" prop="animalNum" />
      <el-table-column label="所属人员" align="center" prop="manId" />
      <el-table-column label="所属实验" align="center" prop="exId" />
      <el-table-column label="笼位类型" align="center" prop="cageTypeId">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cage_type_id" :value="scope.row.cageTypeId"/>
        </template>
      </el-table-column>
      <el-table-column label="笼位规格" align="center" prop="cageSize" />
      <el-table-column label="笼位位置" align="center" prop="position" />
      <el-table-column label="生物安全等级" align="center" prop="safetyLevel">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.safety_level" :value="scope.row.safetyLevel"/>
        </template>
      </el-table-column>
      <el-table-column label="笼位状态" align="center" prop="cageStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cage_status" :value="scope.row.cageStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['lmisCage:cage:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['lmisCage:cage:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改笼位管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="160px">
        <el-row>
        <el-col :span="12">
          <el-form-item label="笼架" prop="cageRackId">
            <el-select v-model="form.cageRackId" placeholder="请选择笼架">
              <el-option
                v-for="dict in dict.type.cage_rack_id"
                :key="dict.value"
                :label="dict.label"
                :value="parseInt(dict.value)"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="在笼动物" prop="animalId">
            <el-input v-model="form.animalId" placeholder="请输入在笼动物" />
          </el-form-item>
        </el-col>
        </el-row>
        <el-row>
        <el-col :span="12">
          <el-form-item label="在笼数量" prop="animalNum">
            <el-input v-model="form.animalNum" placeholder="请输入在笼数量" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="所属人员" prop="manId">
            <el-input v-model="form.manId" placeholder="请输入所属人员" />
          </el-form-item>
        </el-col>
        </el-row>
        <el-row>
        <el-col :span="12">
          <el-form-item label="所属实验" prop="exId">
            <el-input v-model="form.exId" placeholder="请输入所属实验" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="笼位类型" prop="cageTypeId">
            <el-select v-model="form.cageTypeId" placeholder="请选择笼位类型">
              <el-option
                v-for="dict in dict.type.cage_type_id"
                :key="dict.value"
                :label="dict.label"
                :value="parseInt(dict.value)"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        </el-row>
        <el-row>
        <el-col :span="12">
          <el-form-item label="笼位规格" prop="cageSize">
            <el-input v-model="form.cageSize" placeholder="请输入笼位规格" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="笼位位置" prop="position">
            <el-input v-model="form.position" placeholder="请输入笼位位置" />
          </el-form-item>
        </el-col>
        </el-row>
        <el-row>
        <el-col :span="12">
          <el-form-item label="生物安全等级" prop="safetyLevel">
            <el-select v-model="form.safetyLevel" placeholder="请选择生物安全等级">
              <el-option
                v-for="dict in dict.type.safety_level"
                :key="dict.value"
                :label="dict.label"
                :value="parseInt(dict.value)"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="笼位状态" prop="cageStatus">
            <el-select v-model="form.cageStatus" placeholder="请选择笼位状态">
              <el-option
                v-for="dict in dict.type.cage_status"
                :key="dict.value"
                :label="dict.label"
                :value="parseInt(dict.value)"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCage, getCage, delCage, addCage, updateCage } from "../../../api/lmisCage/cage/cage";

export default {
  name: "Cage",
  dicts: ['cage_status', 'safety_level', 'cage_rack_id', 'cage_type_id'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 笼位管理表格数据
      cageList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cageRackId: null,
        animalId: null,
        animalNum: null,
        manId: null,
        exId: null,
        cageTypeId: null,
        cageSize: null,
        position: null,
        safetyLevel: null,
        cageStatus: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询笼位管理列表 */
    getList() {
      this.loading = true;
      listCage(this.queryParams).then(response => {
        this.cageList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        cageId: null,
        cageRackId: null,
        animalId: null,
        animalNum: null,
        manId: null,
        exId: null,
        cageTypeId: null,
        cageSize: null,
        position: null,
        safetyLevel: null,
        cageStatus: null,
        status: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.cageId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加笼位管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const cageId = row.cageId || this.ids
      getCage(cageId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改笼位管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.cageId != null) {
            updateCage(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCage(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const cageIds = row.cageId || this.ids;
      this.$modal.confirm('是否确认删除笼位管理编号为"' + cageIds + '"的数据项？').then(function() {
        return delCage(cageIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('lmisCage/cage/export', {
        ...this.queryParams
      }, `cage_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
